<?php
/**
 * This is the model class for table "{{article}}".
 *
 * The followings are the available columns in table '{{article}}':
 * @property integer $id
 * @property string $module
 * @property integer $module_id
 * @property string $language_code
 * @property string $title
 * @property string $description
 * @property string $content
 * @property string $meta_title
 * @property string $meta_keywords
 * @property string $meta_description
 * @property string $keywords
 * @property string $sef_url
 * @property integer $total_view
 * @property string $create_timezone
 * @property string $create_timestamp
 * @property string $create_datetime
 * @property string $create_date
 * @property string $create_time
 * @property integer $create_by
 * @property string $update_timezone
 * @property string $update_timestamp
 * @property string $update_datetime
 * @property string $update_date
 * @property string $update_time
 * @property integer $update_by
 */
class Content extends ActiveRecord {

    public $moduleId_;
    public $module_;
    public $language_;
    public $title_;
    public $description_;
    public $content_;
    public $sefUrl_;
    public $metaTitle_;
    public $metaDescription_;
    public $metaKeywords_;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Article the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{content}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title_, description_, content_, sefUrl_, metaTitle_, metaDescription_, metaKeywords_, module_', 'filter','filter'=>'trim'),
            //array('title_, description_, content_, sefUrl_, metaTitle_, metaDescription_, metaKeywords_','filter','filter'=>array($obj=new CHtmlPurifier(),'purify')),
            
            array('title_, metaTitle_, sefUrl_', 'length', 'max'=>300),
            array('metaDescription_, metaKeywords_','length','max'=>400),
            array('description_', 'length', 'max'=>500),
            
            array('module_, moduleId_, language_', 'required', 'on'=>'create, adminCreate'),
            array('module_, moduleId_, language_, title_, description_, content_, sefUrl_, metaTitle_, metaDescription_, metaKeywords_', 'safe', 'on' => 'create, update, adminCreate, adminUpdate'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
        );
    }

    /**
     * scopes
     */
    public function module($module = null) {
        $t = $this->getTableAlias(false);
        $this->getDbCriteria()->mergeWith(array(
            'condition' => $t . '.module=:module',
            'params' => array(
                ':module' => strtolower($module),
            ),
        ));
        return $this;
    }

    public function moduleId($id = null) {
        $t = $this->getTableAlias(false);
        $this->getDbCriteria()->mergeWith(array(
            'condition' => $t . '.module_id=:moduleId'.$t,
            'params' => array(
                ':moduleId'.$t => (int)$id,
            ),
        ));
        return $this;
    }
    
    public function moduleIdRange($range=array()){
        if(!is_array($range)|| empty($range)) return $this;
        $t = $this->getTableAlias(false);
        $this->getDbCriteria()->addInCondition($t.'.module_id', $range);
        return $this;
    }

    public function language($language = null) {
        $t = $this->getTableAlias(false);
        $this->getDbCriteria()->mergeWith(array(
            'condition' => $t . '.language_code=:language',
            'params' => array(
                ':language' => $language,
            ),
        ));
        return $this;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id_);
        $criteria->compare('module_id', $this->moduleId_);
        $criteria->compare('module', $this->module_, true);
        $criteria->compare('language_code', $this->language_, true);
        $criteria->compare('title', $this->title_, true);
        $criteria->compare('description', $this->description_, true);
        $criteria->compare('content', $this->content_, true);
        $criteria->compare('sef_url', $this->sefUrl_, true);
        $criteria->compare('meta_title', $this->metaTitle_, true);
        $criteria->compare('meta_keywords', $this->metaKeywords_, true);
        $criteria->compare('meta_description', $this->metaDescription_, true);

        return new ActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function generateSubQuery(){
        $criteria = new CDbCriteria;
        $criteria->alias=$t=strtolower(get_class($this));
        $criteria->select=array($t.'.module_id');
        $criteria->distinct=true;
        
        $criteria->compare($t.'.module', strtolower($this->module_));
        
        if(!empty($this->title_)){
            if(is_array($this->title_)){
                foreach($this->title_ as $keyword){
                    $criteria->addSearchCondition('LOWER('.$t.'.title)', strtolower($keyword), true, 'AND');
                }
            }else{
                $criteria->addSearchCondition('LOWER('.$t.'.title)', strtolower($this->title_));
            }
        }
        
        if(!empty($this->brief_)){
            if(is_array($this->brief_)){
                $criteria->addSearchCondition('LOWER('.$t.'.brief)', strtolower($keyword), true, 'AND');
            }else
                $criteria->addSearchCondition('LOWER('.$t.'.brief)', strtolower($this->brief_));
        }
        
        if(!empty($this->content_)){
            if(is_array($this->content_)){
                foreach($this->content_ as $keyword){
                    $criteria->addSearchCondition('LOWER('.$t.'.content)', strtolower($keyword), true, 'AND');
                }
            }else
                $criteria->addSearchCondition('LOWER('.$t.'.content)', strtolower($this->content_));
        }
        
        if(!empty($this->keywords_)){
            if(is_array($this->keywords_)){
                foreach ($this->keywords_ as $keyword){
                    $criteria->addSearchCondition($t.'.keywords', strtolower($keyword), true, 'AND');
                }
            }else{
                $criteria->addSearchCondition($t.'.keywords', strtolower($this->keywords_));
            }
        }
        
        return $this->criteriaToSQL($this, $criteria);
    }
    
    public function autoFill(&$model=null){
        if($this->isNewRecord){
            $this->module_=get_class($model);
            $this->moduleId_=0;
            $this->language_=$this->defaultLanguage;
        }else{
            $this->title_=$this->title;
            $this->description_=$this->description;
            $this->content_=$this->content;
            $this->sefUrl_=$this->sef_url;
            $this->metaTitle_=$this->meta_title;
            $this->metaDescription_=$this->meta_description;
            $this->metaKeywords_=$this->meta_keywords;
        }
    }
    
    public function getIsEmptyInput(){
        return !count(array_filter(array(
            $this->title_,
            $this->description_,
            $this->content_,
            $this->language_,
            $this->metaDescription_,
            $this->metaKeywords_,
            $this->metaTitle_,
        )));
    }
    
    protected function beforeSave() {
        if (parent::beforeSave()) {

            $this->title = $this->title_;
            $this->description=$this->description_;
            $this->content = $this->content_;
            $this->sef_url = empty($this->sefUrl_)? $this->urlFormat($this->title_): $this->urlFormat($this->sefUrl_);
            $this->meta_title = empty($this->metaTitle_)? $this->keywordFormat($this->title_): $this->keywordFormat($this->metaTitle_);
            $this->meta_description = $this->metaDescription_;
            $this->meta_keywords = $this->metaKeywords_;
            
            $this->keywords = $this->generateKeywords();
            
            if ($this->isNewRecord) {
                $this->module_id = $this->moduleId_;
                $this->module = strtolower($this->module_);
                $this->language_code = $this->language_;
                $this->setCreateDateTime();
            } else {
                $this->setUpdateDateTime();
            }
            return true;
        }
        else
            return false;
    }

    public function generateKeywords() {
        return strtolower(implode(' ', array_filter(array(
            $this->keywordFormat($this->title),
            $this->keywordFormat($this->description),
            $this->keywordFormat($this->content),
            $this->meta_title,
            $this->meta_description,
            $this->meta_keywords,
        ))));
    }
    
    public function getTitle() {
        return $this->title;
    }

    public function getDescription(){
        return CHtml::decode($this->description);
    }
    
    public function getFullContent() {
        return nl2br($this->content);
    }

    public function getSefUrl() {
        return CHtml::encode($this->sef_url);
    }

    public function getMetaTitle() {
        return CHtml::encode($this->meta_title);
    }

    public function getMetaKeywords() {
        return CHtml::encode($this->meta_keywords);
    }

    public function getMetaDescription() {
        return CHtml::encode($this->meta_description);
    }
    
    public function getLanguageCode(){
        return $this->language_code;
    }
    
    public function getModuleId(){
        return $this->module_id;
    }

}//end class